package com.taotao.service.impl;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import com.taotao.common.utils.Constants;
import com.taotao.service.ParamService;
import com.taotao.service.PictureService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import com.taotao.common.pojo.BaseResult;
import com.taotao.mapper.EyCategoryMapper;
import com.taotao.pojo.EyCategory;
import com.taotao.pojo.EyCategoryExample;
import com.taotao.pojo.EyCategoryExample.Criteria;
import com.taotao.service.CategoryService;

@Service
public class CategoryServiceImpl implements CategoryService {

	@Autowired
	EyCategoryMapper eyCategoryMapper;
	@Autowired
	ParamService paramService;
	@Autowired
	PictureService pictureService;
	BaseResult baseResult = new BaseResult();
	
	@Override
	public Map<String, Object> getCategoryList(Map<String, String> map) {
		try {
			String projectName =  map.get("projectName");
			String parentId = map.get("parentId");
			String language = map.get("language");
			String isAdmin = map.get("isAdmin");
			EyCategoryExample example = new EyCategoryExample();
	  		Criteria criteria = example.createCriteria();
	  		example.setOrderByClause(" CAST(show_order AS SIGNED) asc ");
	  		if(StringUtils.isNotBlank(projectName)){
	  			criteria.andProjectNameEqualTo(projectName);
	  		}
	  		if(StringUtils.isNotBlank(parentId)){
	  			criteria.andParentIdEqualTo(Integer.parseInt(parentId));
	  		}
	  		if(!Constants.num_1.equals(isAdmin)){ //非管理员
	  			criteria.andAdminShowNotEqualTo(Constants.num_1);
	  		}

			String tencentCosEnable=paramService.getParam(Constants.tencent_cos_enable);
	  		List<EyCategory> list = eyCategoryMapper.selectByExample(example);
			for(EyCategory c:list){
				c.setCategoryName(c.getCategoryNameEn());
				if(Constants.false_string.equals(tencentCosEnable)) {
					c.setImageUrl(pictureService.changeDomain(c.getImageUrl()));
				}
			}
	  		
			return baseResult.getSuccMap(list);
		} catch (Exception e) {
			e.printStackTrace();
			return baseResult.getErrorJsonObj("网络繁忙，请稍后再试");
		}
	}

}
